package com.example.blog.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.blog.entity.BlogType;
import com.example.blog.mapper.BlogTypeMapper;
import com.example.blog.service.BlogTypeService;

@Service
public class BlogTypeServiceImpl implements BlogTypeService {

    @Autowired
    private BlogTypeMapper blogTypeMapper;
    
    @Override
    @Transactional
    public Map<String, Object> createBlogType(BlogType blogType) {
        Map<String, Object> map = new HashMap<>();
        
        try {
            // 检查分类名是否已存在（同一用户下）
            if (isTypeNameExists(blogType.getTypeName(), blogType.getUserId())) {
                map.put("success", false);
                map.put("message", "该分类名已存在");
                return map;
            }
            
            int result = blogTypeMapper.insert(blogType);
            if (result > 0) {
                map.put("success", true);
                map.put("message", "创建成功");
                map.put("typeId", blogType.getTypeId());
                map.put("blogType", blogType);
            } else {
                map.put("success", false);
                map.put("message", "创建失败");
            }
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", "系统异常：" + e.getMessage());
        }
        
        return map;
    }

    @Override
    @Transactional
    public Map<String, Object> updateBlogType(BlogType blogType) {
        Map<String, Object> map = new HashMap<>();
        
        try {
            // 查询原分类信息
            BlogType originalType = blogTypeMapper.selectByPrimaryKey(blogType.getTypeId());
            if (originalType == null) {
                map.put("success", false);
                map.put("message", "分类不存在");
                return map;
            }
            
            // 如果修改了分类名，需要检查新分类名是否已存在
            if (!originalType.getTypeName().equals(blogType.getTypeName())) {
                if (isTypeNameExists(blogType.getTypeName(), blogType.getUserId())) {
                    map.put("success", false);
                    map.put("message", "该分类名已存在");
                    return map;
                }
            }
            
            int result = blogTypeMapper.updateByPrimaryKey(blogType);
            if (result > 0) {
                map.put("success", true);
                map.put("message", "更新成功");
            } else {
                map.put("success", false);
                map.put("message", "更新失败");
            }
        } catch (Exception e) {
            map.put("success", false);
            map.put("message", "系统异常：" + e.getMessage());
        }
        
        return map;
    }

    @Override
    @Transactional
    public boolean deleteBlogType(Integer typeId) {
        try {
            // 先检查该类型是否被博客引用
            int blogCount = blogTypeMapper.countBlogsByTypeId(typeId);
            
            if (blogCount > 0) {
                // 如果有博客引用此类型，则不允许删除
                return false;
            }
            
            int result = blogTypeMapper.deleteByPrimaryKey(typeId);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public BlogType getBlogTypeById(Integer typeId) {
        return blogTypeMapper.selectByPrimaryKey(typeId);
    }

    @Override
    public List<BlogType> getAllBlogTypes() {
        return blogTypeMapper.selectAll();
    }

    @Override
    public List<BlogType> getBlogTypesByUserId(Integer userId) {
        return blogTypeMapper.selectByUserId(userId);
    }

    @Override
    public BlogType getBlogTypeByNameAndUserId(String typeName, Integer userId) {
        return blogTypeMapper.selectByTypeNameAndUserId(typeName, userId);
    }

    @Override
    public boolean isTypeNameExists(String typeName, Integer userId) {
        return blogTypeMapper.checkTypeNameExists(typeName, userId) > 0;
    }
} 